home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / renaisnc / lib.lha / Lib / mstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-04  |  2.1 KB  |  132 lines

  1. /*
  2. ** Implementation Module: mstack
  3. ** Author: Tony DeRose
  4. ** Last Modified: 22 May 1985
  5. ** Purpose: matrix stack manipulation.
  6. */
  7. #include <stdio.h>
  8. #include "4D.h"
  9. #include "mstack.h"
  10. #include "fast.c"
  11.  
  12. /* Imports */
  13. extern char *malloc();
  14.  
  15. /* Implementation of MStacks */
  16. typedef struct {
  17.     MstackDisipline disipline;
  18.     int top;
  19.     TransformType4D S[MSTACKSIZE];
  20. } MstackType;
  21.  
  22. /*
  23. ** Create, initialize, and return a matrix stack of the
  24. ** specified disipline.
  25. */
  26. Mstack MsCreate(d)
  27. MstackDisipline d;
  28. {
  29.     MstackType *S = (MstackType *) malloc(sizeof(MstackType));
  30.  
  31.     S->disipline = d;
  32.     S->top = -1;
  33.     return (Mstack) S;
  34.   }
  35.  
  36.  
  37. /*
  38. ** Push Top(S)*T or T*Top(S) onto Mstack S.
  39. */
  40. void MsPush( S, T)
  41. Mstack S;
  42. TransformType4D T;
  43. {
  44.   MstackType *s = (MstackType *) S;
  45.   TransformType4D NewTop, Stop;
  46.  
  47.   if (s->top == (MSTACKSIZE-1)) {
  48.     perror("Matrix stack overflow\n");
  49.     exit(-1);
  50.   }
  51.  
  52.   if (s->top == -1) {
  53.     /* Do a simple push onto the already empty stack s */
  54.     NewTop = T;
  55.   } else {
  56.     /* Either pre or post multiply */
  57.       Stop = s->S[s->top];
  58.       if (s->disipline == Premultiply) {
  59.     TxT4Dm(NewTop,T,Stop);
  60.       } else {
  61.     TxT4Dm(NewTop,Stop,T);
  62.       }
  63.   }
  64.   s->top++;
  65.   s->S[s->top] = NewTop;
  66. }
  67.  
  68.  
  69.  
  70. void MsPop(S)
  71. Mstack S;
  72. {
  73.   MstackType *s = (MstackType *) S;
  74.  
  75.   if (s->top == -1) {
  76.     /* Can't pop an empty stack */
  77.     perror("Matrix stack underflow\n");
  78.     exit(-1);
  79.   }
  80.   s->top--;
  81. }
  82.  
  83. TransformType4D MsTop(S)
  84. Mstack S;
  85. {
  86.   MstackType *s = (MstackType *) S;
  87.  
  88.   if (s->top == -1) {
  89.     perror("Can't take top of an empty stack\n");
  90.   }
  91.   return s->S[s->top];
  92. }
  93.  
  94. /*
  95. ** Return the point P transformed by the top of the stack S.
  96. */
  97. PointType4D PxTop(P,S)
  98. PointType4D P;
  99. Mstack S;
  100. {
  101.   MstackType *s = (MstackType *) S;
  102.  
  103.   return PxT4D(P,s->S[s->top]);
  104. }
  105.  
  106. /*
  107. ** Return the ray R transformed by the top of S.
  108. */
  109. /*Ray RayxTop(R,S)
  110. Ray R;
  111. Mstack S;
  112. {
  113.   MstackType *s = (MstackType *) S;
  114.  
  115.   return RayxT4D(R,s->S[s->top]);
  116. }
  117. */
  118. void MsPrint(f,S)
  119. FILE *f;
  120. Mstack S;
  121. {
  122.   MstackType *s = (MstackType *) S;
  123.   int i;
  124.  
  125.   for (i = s->top; i>-1; i--) {
  126.     PrintTransform4D(f,s->S[i]);
  127.     fprintf(f,"---------------------------\n");
  128.   }
  129. }
  130.  
  131.            
  132.